package com.taokatao.cardmee.db.upgrade;

import com.google.gson.reflect.TypeToken;
import com.taokatao.cardmee.App;
import com.taokatao.cardmee.Utils;
import com.taokatao.cardmee.db.CardGradeDao;
import com.taokatao.cardmee.db.CardStoreDao;
import com.taokatao.cardmee.db.model.CardGrade;
import com.taokatao.cardmee.db.model.CardStore;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.schedulers.Schedulers;

/**
 * Created by LY on 2016/8/16.
 */
public class CardStoreDBDataImporter {
    public static void importData() {
        Observable.fromCallable(new Func0<Boolean>() {
            @Override
            public Boolean call() {
                CardStoreDao cardStoreDao = App.i().getCommonDBSession().getCardStoreDao();
                boolean condition1 = cardStoreDao.queryBuilder().count() < 1;
                CardGradeDao cardGradeDao = App.i().getCommonDBSession().getCardGradeDao();
                boolean condition2 = cardGradeDao.queryBuilder().count() < 1;
                return condition1 || condition2;
            }
        }).observeOn(Schedulers.io()).subscribeOn(Schedulers.io()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                if (aBoolean) {
                    try {
                        CardStoreDao cardStoreDao = App.i().getCommonDBSession().getCardStoreDao();
                        cardStoreDao.deleteAll();
                        CardGradeDao cardGradeDao = App.i().getCommonDBSession().getCardGradeDao();
                        cardGradeDao.deleteAll();
                        List<CardStore> cardStoreList = initBasicCardStoreFromLocalData();
                        if (cardStoreList != null) {
                            cardStoreDao.insertOrReplaceInTx(cardStoreList);
                            for (CardStore cs : cardStoreList) {
                                List<CardGrade> cardGradeList = cs.getCardsGradeList();
                                if (cardGradeList != null) {
                                    cardGradeDao.insertOrReplaceInTx(cardGradeList);
                                }
                            }
                            Utils.logd("CardStoreDBDataImporter The local card store data import completed [%d]", cardStoreList.size());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Utils.loge("CardStoreDBDataImporter import failure");
                    }
                }
            }
        });
    }

    private static List<CardStore> initBasicCardStoreFromLocalData() throws Exception {
        InputStreamReader inputReader = new InputStreamReader(App.i().getAssets().open("cache_card_store_list_2_1479705169478.json"));
        BufferedReader bufReader = new BufferedReader(inputReader);
        String line;
        StringBuilder result = new StringBuilder();
        while ((line = bufReader.readLine()) != null) {
            result.append(line);
        }
        List<CardStore> cardStoreList = App.i().getGson().fromJson(result.toString(), new TypeToken<List<CardStore>>() {
        }.getType());
        return cardStoreList;
    }
}
